package gr.gsis.efmy.view;

import java.util.logging.Logger;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class DetailsGroup extends Composite {
	
	private static Logger LOGGER = Logger.getLogger("DetailsGroup");
	
	private Group group;
	
	private Text txtYear;
	
	private Text txtAfm;
	
	private Text txtBusiness;
	
	private Text txtCompanyName;
	
	private Text txtFirstName;
	
	private Text txtLastName;
	
	private Text txtCity;
	
	private Text txtZipCode;
	
	private Text txtStreet;
	
	private Text txtNumber;
	
	public DetailsGroup(Composite parent) {
		super(parent, SWT.CASCADE);
		init();
	}
	
	private void init() {
		this.setLayout(new FillLayout());
		group = new Group(this, SWT.SHADOW_ETCHED_IN);
		group.setText("Details Information");
		group.setLayout(new GridLayout(4, false));
		createFields();
	}
	
	private void createFields() {
		
		Label lblYear = createLabel("Έτος που αφορούν τα στοιχεία : (*)");
		txtYear = createText();
		
		Label lblAfm = createLabel("ΑΦΜ: (*)");
		txtAfm = createText();
		
		Label lblBusiness = createLabel("Αντικείμενο δραστηριότητας επιχείρησης: (*)");
		txtBusiness = createText();
		
		Label empty1 = createEmptySpaner(2);
		
		final Button btnCompanyName = createRadio("Επωνυμία");

		Label lblCompanyName = createLabel("Επωνυμία Επιχείρησης: (*)");
		GridData gridData = new GridData();
		gridData.horizontalIndent = 30;
		lblCompanyName.setLayoutData(gridData);
		lblCompanyName.setEnabled(false);
		txtCompanyName = createText();
		txtCompanyName.setEnabled(false);
		
		Label empty2 = createEmptySpaner(2);
		
		final Button btnFullName = new Button(group, SWT.RADIO);
		btnFullName.setText("Ονοματεπώνυμο");
		gridData = new GridData();
		gridData.horizontalSpan = 4;
		btnFullName.setLayoutData(gridData);
		
		Label lblFirstName = createLabel("Όνομα:");
		txtFirstName = createText();
		txtFirstName.setEnabled(false);

		Label lblLastName = createLabel("Επώνυμο:");
		txtLastName = createText();
		txtLastName.setEnabled(false);
		
		SelectionListener listener = new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				toggleNames();
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				toggleNames();
			}
			
			private void toggleNames() {
				LOGGER.info("Name toggled!");
				boolean isCompanySelected = btnCompanyName.getSelection();
				LOGGER.info("CompanyName: " + btnCompanyName.getSelection());
				boolean isFullNameSelected = btnFullName.getSelection();
				LOGGER.info("FullName: " + btnFullName.getSelection());
				if (isCompanySelected) {
					txtFirstName.setText("");
					txtFirstName.setEnabled(false);
					txtLastName.setText("");
					txtLastName.setEnabled(false);
					txtCompanyName.setEnabled(true);
					txtCompanyName.setFocus();
				}
				if (isFullNameSelected) {
					txtCompanyName.setText("");
					txtCompanyName.setEnabled(false);
					txtFirstName.setEnabled(true);
					txtLastName.setEnabled(true);
					txtFirstName.setFocus();
				}
			}
		};
		
		btnCompanyName.addSelectionListener(listener);
		btnFullName.addSelectionListener(listener);
		
		Label lblAddress = createLabel("Διεύθυνση");
		gridData = new GridData();
		gridData.horizontalSpan = 4;
		lblAddress.setLayoutData(gridData);
		
		Label lblCity = createLabel("Πόλη:");
		txtCity = createText();
		
		Label lblZipCode = createLabel("Τ.Κ.:");
		txtZipCode = createText();
		
		Label lblStreet = createLabel("Οδός: ");
		txtStreet = createText();
		
		Label lblNumber = createLabel("Αριθμός: ");
		txtNumber = createText();

		Label lblRequiredFields = createLabel("(*) Υποχρεωτικά πεδία");
		
	}

	private Label createLabel(String labelName) {
		Label label  = new Label(group, SWT.LEFT);
		label.setText(labelName);
		return label;
	}
	
	private Text createText() {
		Text text = new Text(group, SWT.BORDER);
		text.setLayoutData(new GridData(
				GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
		return text;
	}
	
	private Label createEmptySpaner(int spanCols) {
		Label empty = new Label(group, SWT.NONE);
		GridData gridData = new GridData(
				GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
		gridData.horizontalSpan = spanCols;
		empty.setLayoutData(gridData);
		
		return empty;
	}
	
	private Button createRadio(String labelName) {
		Button button = new Button(group, SWT.RADIO);
		button.setText(labelName);
		GridData gridData = new GridData();
		gridData.horizontalSpan = 4;
		button.setLayoutData(gridData);
		return button;
	}

	public Text getTxtYear() {
		return txtYear;
	}

	public Text getTxtAfm() {
		return txtAfm;
	}

	public Text getTxtBusiness() {
		return txtBusiness;
	}

	public Text getTxtCompanyName() {
		return txtCompanyName;
	}

	public Text getTxtFirstName() {
		return txtFirstName;
	}

	public Text getTxtLastName() {
		return txtLastName;
	}

	public Text getTxtCity() {
		return txtCity;
	}

	public Text getTxtZipCode() {
		return txtZipCode;
	}

	public Text getTxtStreet() {
		return txtStreet;
	}

	public Text getTxtNumber() {
		return txtNumber;
	}
}
